package com.gaoyang.service;

import com.gaoyang.domain.DianPingUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DianpingUserService {

	private static String PRODUCT_DIANPING_USER_COLLECTION = "dianping_user";
	private static String PRODUCT_MEITUAN_USER_COLLECTION = "dianping_user_copy1";

	@Autowired
    MongoTemplate mongoTemplate;

	public DianPingUser findDianpingUserByTel(String tel) {
		return mongoTemplate.findOne(new Query(Criteria.where("tel").is(tel)), DianPingUser.class, PRODUCT_DIANPING_USER_COLLECTION);
	}

	public List<DianPingUser> getDianPingList() {
		return mongoTemplate.findAll(DianPingUser.class, PRODUCT_DIANPING_USER_COLLECTION);
	}

	public List<DianPingUser> getDianPingListExecute() {
		return mongoTemplate.find(new Query(Criteria.where("isExecute").is("1")), DianPingUser.class, PRODUCT_DIANPING_USER_COLLECTION);
	}

	public List<DianPingUser> getMeiTuanListExecute() {
		return mongoTemplate.find(new Query(Criteria.where("isExecute").is("1")), DianPingUser.class, PRODUCT_MEITUAN_USER_COLLECTION);
	}

	public void update(String tel, String column, String value) {
		Query query = new Query(Criteria.where("tel").is(tel));
		Update update = Update.update(column, value);
		this.mongoTemplate.updateFirst(query, update, PRODUCT_DIANPING_USER_COLLECTION);
	}


}